package com.xiaolei.library.Net.okhttputil.interceptor;


import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xiaolei on 2017/5/10.
 */

public class HeaderInterceptor implements Interceptor
{
    /**
     * 必须的请求头
     */
    private Map<String, String> defaultHeaders = new ConcurrentHashMap<>();
    
    public Map<String, String> getDefaultHeaders()
    {
        return defaultHeaders;
    }

    @Override
    public Response intercept(Chain chain) throws IOException
    {
        Request.Builder builder = chain.request().newBuilder();
        if (defaultHeaders != null)// 将请求头添加至网络请求中
        {
            for (Map.Entry<String, String> entry : defaultHeaders.entrySet())
            {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        Request request = builder.build();
        return chain.proceed(request);
    }
}
